package com.lx.service.impl;

import com.lx.service.IMailService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.File;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

/**
 * 邮件发送服务类
 *
 * @author 段誉
 * @create 2019-04-08 19:20
 */
@Service
public class MailServiceImpl implements IMailService {

  /**
   * 发送邮件接口
   */
  @Autowired
  private JavaMailSender jms;

  /**
   * 发送模板邮件时解析模板
   */
  @Autowired
  private TemplateEngine templateEngine;

  /**
   * 读取配置文件中的邮件发送者账号
   */
  @Value("${spring.mail.username}")
  private String from;

  /**
   * 简单文本邮件
   *
   * @param toUser 邮件接收者
   */
  @Override
  public void simpleMil(String toUser) {
    //初始化简单的邮件对象
    SimpleMailMessage message = new SimpleMailMessage();
    //邮件发送者
    message.setFrom(from);
    //邮件接收者
    message.setTo(toUser);
    //邮件标题
    message.setSubject("文字格式邮件");
    //邮件内容
    message.setText("文字格式内容");
    //发送邮件
    jms.send(message);
  }

  /**
   * html邮件
   */
  @Override
  public void htmlMail(String toUser) throws MessagingException {
    MimeMessage message = jms.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(message, true);
    helper.setFrom(from);
    helper.setTo(toUser);
    helper.setSubject("html格式标题");
    String content = "<p style='color:yellow;'>这是一封html格式的文件</p><h1>这是一封html格式的文件</h1>";
    //true表示以html格式发送邮件
    helper.setText(content, true);
    jms.send(message);
  }

  /**
   * 带附件邮件
   */
  @Override
  public void attachmentMail(String toUser) throws MessagingException {
    MimeMessage message = jms.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(message, true);
    helper.setFrom(from);
    helper.setTo(toUser);
    helper.setSubject("带附件");
    helper.setText("这是一封带附件的邮件");
    //加载文件路径
    FileSystemResource fs = new FileSystemResource(new File(this.getClass().getResource("/").getPath()
            + "\\自己菜与大佬菜的区别.jpg"));
    //添加附件
    helper.addAttachment("自己菜与大佬菜的区别.jpg", fs);
    jms.send(message);
  }

  /**
   * 带图片邮件
   */
  @Override
  public void imgMail(String toUser) throws MessagingException {
    MimeMessage message = jms.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(message, true);
    helper.setFrom(from);
    helper.setTo(toUser);
    helper.setSubject("带图片邮件");
    //设置资源的cid
    String content = "<html><body>小菜鸡图片<img src='cid:img'/></body></html>";
    helper.setText(content, true);
    FileSystemResource fs = new FileSystemResource(new File(this.getClass().getResource("/").getPath()
            + "\\自己菜与大佬菜的区别.jpg"));
    helper.addInline("img", fs);
    jms.send(message);
  }

  /**
   * 模板邮件
   */
  @Override
  public void templateMail(String toUser) throws MessagingException {
    MimeMessage message = jms.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(message, true);
    helper.setFrom(from);
    helper.setTo(toUser);
    helper.setSubject("模板邮件");
    Context context = new Context();
    context.setVariable("username", "段誉");
    //thymeleaf模板默认会从src/resources/templates目录下寻找文件，填入我们定义的模板名，不需要写后缀
    String template = templateEngine.process("MailTemplate", context);
    helper.setText(template, true);
    jms.send(message);
  }
}
